package io.bluerain.resource.ui;

import android.app.Activity;
import android.graphics.drawable.ClipDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

import io.bluerain.resource.R;

/**
 * Created by foredawn on 15-9-17.
 * Drawable中的图片截取 模仿进度条效果
 */
public class ClipActivity extends Activity {
    ClipDrawable cd;
    ImageView img;
    Handler handler;
    static int HAND_LEVEL = 0x1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clip_activity);
        cd = (ClipDrawable) getResources().getDrawable(R.drawable.clip_drawable);
        img = (ImageView) findViewById(R.id.img);
        img.setImageDrawable(cd);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == HAND_LEVEL) {
                    cd.setLevel(cd.getLevel() + 200);
                }
            }
        };
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (cd.getLevel() == 10000)
                    timer.cancel();
                else {
                    handler.sendEmptyMessage(HAND_LEVEL);
                }
            }
        }, 0, 100);
    }
}
